<?php

namespace Boosen\Admin\Contents\Controllers;

use Boosen\Admin\Contents\Models\Page;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Boosen\Admin\Contents\Extensions\Tools\Trashed;

class PageController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('header'));
            $content->description(__('description'));

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(__('header'));
            $content->description(__('description'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('header'));
            $content->description(__('description'));
            
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Page::class, function (Grid $grid) {
            if (request('trashed') == 1) {
                $grid->model()->onlyTrashed();
            }

            $grid->id('ID')->sortable();
            $grid->title()->ucfirst()->limit(30);

            $grid->cover()->image();
            $states = [
                'on' => ['value' => 1, 'text' => 'YES'],
                'off' => ['value' => 0, 'text' => 'NO'],
            ];

            $grid->released()->switch($states);

            $grid->created_at();

            $grid->filter(function (Grid\Filter $filter) {

                $filter->equal('title');
                $filter->equal('released')->radio([
                    ''   => 'All',
                    0    => 'Unreleased',
                    1    => 'Released',
                ]);

                $filter->equal('created_at')->datetime();

                $filter->between('updated_at')->datetime();

            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
            });
            $grid->tools(function ($tools) {

                $tools->append(new Trashed());

            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        Admin::script($this->script());
        return Admin::form(Page::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('title', 'Title')->rules('required');
            $form->text('slug', 'Slug')->rules('required')->attribute(['readonly' => 'readonly'])->help(' slug will use for uri.');

            $form->text('keywords', 'Keywords');
            $form->textarea('short_description', 'Description')->rows(8);
            $form->image('cover', 'Cover')->uniqueName();
            $form->editor('content', 'Content');
            
            $states = [
                'on' => ['value' => 1, 'text' => 'YES'],
                'off' => ['value' => 0, 'text' => 'NO'],
            ];
            $form->switch('released')->states($states);
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
            $form->disableViewCheck();
            $form->disableEditingCheck();
        });
    }


    private function script()
    {
        return <<<EOT
(function($) {
    $.fn.clickToggle = function(func1, func2) {
        var funcs = [func1, func2];
        this.data('toggleclicked', 0);
        this.click(function() {
            var data = $(this).data();
            var tc = data.toggleclicked;
            $.proxy(funcs[tc], this)();
            data.toggleclicked = (tc + 1) % 2;
        });
        return this;
    };
}(jQuery));

var oSlug = $('#slug').val();
var titleMonitor = function() {
    var title = $('#title').val();
    $('#slug').val(title.toLowerCase().replace(/\s+/g, '-'));
};

if (!oSlug) {
    titleMonitor();
}

$('#title').keyup(function(e) {
    titleMonitor();
});
$('#title').change(function(e) {
    titleMonitor();
});

var lock = '<i class="fa fa-lock"></i>';
var unlock = '<i class="fa fa-unlock"></i>';

$('#slug').css('background-color', 'rgb(243, 243, 243)').prev().css('cursor', 'pointer').html(lock).clickToggle(function() {
    $(this).next('#slug').css('background-color', '#ffffff').prop('readonly', false);
    $(this).html(unlock);
}, function() {
    $(this).next().css('background-color', 'rgb(243, 243, 243)').prop('readonly', true);
    $(this).html(lock);
});
EOT;
    }
}
